<?php

/**
 * 商户-订单导入管理
 * @author chenshubo
 */
namespace app\controller;

use app\base\UserBase;
use App\Service\Code\Normal;
use Ly\Model\ImportOrder;
use Ly\Unit\Export\Export;
use Ly\Model\CommodityServiceModel\Warehouseinfo;
use Ly\Model\MerchantStoreModel;
use Ly\Model\OrderSupplier;

class OrderimportController extends UserBase
{    
    /**
     * 订单列表
     */
    public function orderList()
    {
        $this->param['order_type'] = 2;
        $res  = OrderSupplier::instance()->getOrderList($this->param);
        return $this->json($res);
    }
    
    
    /**
     * 获取仓库列表
     */
    public function getWareHouseLists()
    {
        $res = MerchantStoreModel::instance()->getStoreListByBid($this->param);
        if( empty($res) ) {
            return $this->json(Normal::returnCode(Normal::FAIL,"暂未授权仓库"));
        }
        
        $res = Warehouseinfo::instance()->getUserWareHouseListByIds(array_column($res, 'store_id'));
        return $this->json($res);
    }
    
    /**
     * 下载导入订单模板
     */
    public function downimportMerchantOrderDemo(){
        $header = [
            '商户单号（文本格式）',
            '商品SKU（文本格式）',
            '商品校期（文本格式,如无校期，则填写“基准批次”）',
            '商品包装（件装（注意：为 数字整数 ，比如 ‘一罐装 ’ 填写  ‘1’ ））',
            '商品数量（文本格式）',
            '收货人真实姓名（文本格式）',
            '收货人身份证（文本格式）',
            '收货人电话（文本格式）',
            '收货地址（省）',
            '收货地址（市）',
            '收货地址（区/县）',
            '收货地址（详细地址）'
        ];
        Export::downXls(['file_name' => "订单模板", 'header' => $header, 'data' => []]);
    }
    
    /**
     * 上传订单数据
     */
    public function importMerchantOrder() 
    {
        $file = $this->request->file('file');
        if(empty($file)) {
            return $this->json(Normal::returnCode(Normal::FAIL,"请先上传文件"));
        }
    
        $info = $file->validate(['ext'=>'xls,xlsx'])->move(\think\facade\Env::get('root_path') . 'public' . DIRECTORY_SEPARATOR . 'uploads'. DIRECTORY_SEPARATOR .'merchantorder');
        if(!$info) {
            return $this->json(Normal::returnCode(Normal::FAIL,"文件不存在或者文件格式错误"));
        }
        
        $savename = $info->getSaveName();
        $savepath = 'public' . DIRECTORY_SEPARATOR . 'uploads'. DIRECTORY_SEPARATOR .'merchantorder'. DIRECTORY_SEPARATOR .$savename;
        $list     = ExcelImport(\think\facade\Env::get('root_path').$savepath,$info->getExtension());
        
        /* $savename = '3.xls';
        $savepath = 'public' . DIRECTORY_SEPARATOR . 'uploads'. DIRECTORY_SEPARATOR .'merchantorder'. DIRECTORY_SEPARATOR .$savename;
        $list     = ExcelImport(\think\facade\Env::get('root_path').$savepath,'xls'); */
        $res = ImportOrder::instance()->ImportOrderData([
            'storeCode' => $this->request->param('storeCode'),
            'list'      => $list,
            'binfo'     => $this->param['binfo'],
            'file_name' => $info->getInfo('name')
        ]);
        
        return $this->json($res);
    }
    
    /**
     * 获取导入任务列表
     */
    public function getImportTaskLog()
    {
        $res = ImportOrder::instance()->getImportList($this->param);
        return $this->json($res);
    }
    
    /**
     * 获取当前导入记录明细
     */
    public function getCurrentImportOrderList()
    {
        $res = ImportOrder::instance()->getCurrentImportOrderList($this->param);
        return $this->json($res);
    }
    
    /**
     * 获取当前导入记录订单商品记录
     */
    public function getCurrentImportOrderGoodsList()
    {
        $res = ImportOrder::instance()->getCurrentImportOrderGoodsList($this->param);
        return $this->json($res);
    }
    
    /**
     * 管理列表导出
     */
    public function userImportOrderExport()
    {
        $this->param['order_type'] = 2;
        $res  = OrderSupplier::instance()->userImportOrderExport($this->param);
    }
}
